using Microsoft.EntityFrameworkCore;
using Project_Schedule.Configures;
using Project_Schedule.Domain.BackGround.com.core.dto;
using Project_Schedule.Domain.BackGround.com.core.Interface;
using Project_Schedule.Domain.Request;
using Project_Schedule.Model;

namespace Project_Schedule.Domain.BackGround.com.core.Service;

public class Bg_RegisterService : IBackgroundRegister
{
    public async Task<JRequest> BgRegister(BackgroundRegister dto)
    {
        try
        {
            await using var db = new DataDbContext();
            var info = await db.BackgroundUsers?.FirstOrDefaultAsync(e =>
                e.Account == dto.Account);
            if (info is null)
                throw new Exception("账户错误！没有检测到此账户哦");
            var password = RsaEncryptUtil.RSADecrypt(info.Password!);
            if (password != dto.Password)
                throw new Exception("密码错误！");
            return JRequest.Ok(new { info.Auth, info.UserName });
        }
        catch (Exception exception)
        {
            _logHelper.LogError(exception);
            return JRequest.Ok(null!, false, exception.Message);
        }
    }

    private readonly INLogHelper _logHelper;

    public Bg_RegisterService(INLogHelper logHelper)
    {
        _logHelper = logHelper;
    }
}